package com.xingxue.logistics.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xingxue.logistics.common.vo.RestResult;
import com.xingxue.logistics.dao.domain.SysRoleUser;
import com.xingxue.logistics.dao.domain.SysRoleUserExample;
import com.xingxue.logistics.dao.domain.SysUser;
import com.xingxue.logistics.dao.domain.SysUserExample;
import com.xingxue.logistics.dao.mapper.SysRoleMapper;
import com.xingxue.logistics.dao.mapper.SysRoleUserMapper;
import com.xingxue.logistics.dao.mapper.SysUserMapper;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户服务
 * @author Kris at 2018/9/29 11:35
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysRoleMapper sysRoleMapper;
    @Autowired
    SysRoleUserMapper sysRoleUserMapper;

    @Override
    public SysUser selectUserByAccount(String account) {
        SysUserExample example = new SysUserExample();
        example.createCriteria().andAccountEqualTo(account);
        List<SysUser> users = sysUserMapper.selectByExample(example);
        if (users == null || users.isEmpty()) {
            return null;
        }
        return users.get(0);
    }

    @Override
    public RestResult<SysUser> doLogin(String account,String pwd) {
        SysUserExample example = new SysUserExample();
        example.createCriteria()
                .andAccountEqualTo(account)
                .andPwdEqualTo(pwd);
        List<SysUser> users = sysUserMapper.selectByExample(example);
        if(users == null || users.isEmpty()){
            return new RestResult<>(false,"用户名或密码错误",null);
        }
        SysUser currenUser = users.get(0);
        currenUser.setPwd("");
        return new RestResult<>(true,"登陆成功",currenUser);
    }

    @Override
    public PageInfo<SysUser> searchPage(Integer id, String nick, String account, Integer pageNum, Integer pageSize) {
        SysUserExample example = new SysUserExample();
        example.setOrderByClause("id asc");
        SysUserExample.Criteria criteria = example.createCriteria();
        if(id != null){
            criteria.andIdEqualTo(id);
        }
        if(StringUtils.isNotBlank(nick)){
            criteria.andNickLike("%"+nick+"%");
        }
        if(StringUtils.isNotBlank(account)){
            criteria.andAccountLike("%"+account+"%");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> sysUsers = sysUserMapper.selectByExample(example);
        return new PageInfo<SysUser>(sysUsers);
    }

    @Override
    public RestResult add(SysUser user) {
        int i = sysUserMapper.insertSelective(user);
        if(i>0){
            return new RestResult(true,"新增成功",null);
        }
        return new RestResult(false,"新增失败",null);
    }

    @Override
    public RestResult update(SysUser user) {
        int i = sysUserMapper.updateByPrimaryKey(user);
        if(i>0){
            return new RestResult(true,"修改成功",null);
        }
        return new RestResult(false,"修改失败",null);
    }

    @Override
    public RestResult delete(Integer id) {
        int i = sysUserMapper.deleteByPrimaryKey(id);
        if(i>0){
            return new RestResult(true,"删除成功",null);
        }
        return new RestResult(false,"删除失败",null);
    }

    @Override
    public List<SysUser> loadUserByRoleId(Integer roleId) {
            return sysUserMapper.loadUserByRoleId(roleId);
    }

    @Override
    public void updateUserByRoleId(Integer roleId, Integer[] userIdsArr) {
        SysRoleUserExample sysRoleUserExample=new SysRoleUserExample();
        sysRoleUserMapper.deleteByExample(sysRoleUserExample);
        for(int i=0;i<userIdsArr.length;i++){
            SysRoleUser sysRoleUser=new SysRoleUser();
            sysRoleUser.setRoleId(roleId);
            sysRoleUser.setUserId(userIdsArr[i]);
            sysRoleUserMapper.insert(sysRoleUser);
        }
    }

    @Override
    public List<SysUser> loadAll() {
        SysUserExample example=new SysUserExample();
        example.setOrderByClause(" id asc");
        return sysUserMapper.selectByExample(example);
    }
}
